/*
 * Copyright (C) 2017 Fabian Terhorst
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file kt in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.fabianterhorst.isometric;

import io.reactivex.rxjava3.annotations.NonNull;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.app.Context;

/**
 * IsometricView.
 *
 * @since 2021-03-11
 */

public class IsometricView extends Component implements Component.DrawTask {
    /**
     * To get class name
     */
    private static final String TAG = IsometricView.class.getName();

    private boolean sort = true;

    private boolean cull = false;

    private boolean boundsCheck = false;

    private boolean reverseSortForLookup = false;

    private boolean touchRadiusLookup = false;

    @Override
    public void onDraw(Component component, Canvas canvas) {
        isometric.draw(canvas, getWidth(), getHeight());
        HiLogs.d(TAG, "Component :" + component+"Canvas :" + canvas);
    }
    /**
     * interface OnItemClickListener
     */
    /**
     * interface OnItemClickListener.
     *
     * @since 2021-03-11
     */

    public interface OnItemClickListener {
        void onClick(@NonNull Isometric.Item item);
    }

    private final Isometric isometric = new Isometric();

    /**
     * OnItemClickListener listener
     */
    private OnItemClickListener listener;

    private double touchRadius = 1;

    /**
     * IsometricView
     *
     * @param context {@link Context}
     */
    public IsometricView(Context context) {
        super(context);
        HiLogs.d(TAG, context.toString());
        HiLogs.d(TAG, "Context :" + context);
    }

    /**
     * IsometricView
     *
     * @param context {@link Context}
     * @param attrs {@link AttrSet}
     */
    public IsometricView(Context context, AttrSet attrs) {
        super(context, attrs);
        HiLogs.d(TAG, "Attrs Set Names  :" + attrs+"Context :" + context);
        addDrawTask(this);
    }

    /**
     * constructor
     *
     * @param context {@link Context}
     * @param attrSet {@link AttrSet}
     * @param styleName {@link String}
     */
    public IsometricView(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        for (int i = 0; i < attrSet.getLength(); i++) {
            HiLogs.d(TAG, "Attrs Set Names  :" + attrSet.getAttr(i).get().getName());
        }
        addDrawTask(this);
    }

    /**
     * constructor
     *
     * @param context {@link Context}
     * @param attrSet {@link AttrSet}
     * @param resId {@link int}
     */
    public IsometricView(Context context, AttrSet attrSet, int resId) {
        super(context, attrSet, resId);
        for (int i = 0; i < attrSet.getLength(); i++) {
            HiLogs.d(TAG, "Attrs Set Names  :" + attrSet.getAttr(i).get().getName());
        }
        addDrawTask(this);
    }

    /**
     * setSort
     *
     * @param sort {@link boolean}
     */
    public void setSort(boolean sort) {
        this.sort = sort;
    }

    /**
     * This greatly improves drawing speed
     * Paths must be defined in a counter-clockwise rotation order
     *
     * @param cull {@link boolean}
     */
    public void setCull(boolean cull) {
        this.cull = cull;
    }

    /**
     * This improves drawing speed by not considering items that are outside of view bounds
     *
     * @param boundsCheck {@link boolean}
     */
    public void setBoundsCheck(boolean boundsCheck) {
        this.boundsCheck = boundsCheck;
    }

    /**
     * This items array is normally sorted back-to-front for drawing purposes. This allows the
     * items array to be reversed when looking up which drawing item was touched.
     *
     * @param reverseSortForLookup {@link boolean}
     */
    public void setReverseSortForLookup(boolean reverseSortForLookup) {
        this.reverseSortForLookup = reverseSortForLookup;
    }

    /**
     * Allow the click lookup to consider a touch region defined by a circle instead of a fixed point
     *
     * @param touchRadiusLookup {@link boolean}
     */
    public void setTouchRadiusLookup(boolean touchRadiusLookup) {
        this.touchRadiusLookup = touchRadiusLookup;
    }

    /**
     * Radius of circular region with the center being the click event location.
     * Size in screen pixels.
     *
     * @param touchRadius {@link double}
     */
    public void setTouchRadius(double touchRadius) {
        this.touchRadius = touchRadius;
    }

    /**
     * setClickListener
     *
     * @param listener {@link OnItemClickListener}
     */
    public void setClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    /**
     * To clear view
     */
    public void clear() {
        isometric.clear();
        HiLogs.d(TAG,"clear()");
    }

    /**
     * add() component
     *
     * @param path {@link Path}
     * @param color {@link Color}
     */
    public void add(Path path, Color color) {
        HiLogs.d(TAG,"add() path : "+path+"add() color : "+color);
        isometric.add(path, color);
    }

    /**
     * add()
     *
     * @param shape {@link Shape}
     * @param color {@link Color}
     */
    public void add(Shape shape, Color color) {
        HiLogs.d(TAG, "add() shape : " +shape + "  add() color  : " +color);
        isometric.add(shape, color);
    }
}